﻿Ext.define('HIT.view.SetUserRightsPermissionPanel', {
    extend: 'Ext.form.Panel',
    alias: 'widget.setuserrightspermissionpanel',

    height: 350,
    width: 400,
    overlapHeader: false,
    preventHeader: true,
    layout: {
        type: 'absolute'
    },
    title: Share.T('数据权限'),

    initComponent: function() {
        var me = this;
        this.title = Share.T('数据权限');
        Ext.applyIf(me, {
            items: [
			{
			    xtype: 'hiddenfield',
			    id: 'functionId',
			    value: 1
			 }, 
			{
				xtype: 'hiddenfield',
                id: 'userId'
			},
		    {
				  xtype: 'treepanel',
				  id   :'userRights-permission-tree',
				  x:0,
				  y:30,
                  height: 280,
                  bodyBorder: false,                  
                  titleCollapse: false,
                  store: 'PermissionTree',                
                  folderSort: true,
                  rootVisible: true,
                  listeners: {  
              	       itemclick: function(view, record, item, index, e){  
                      	if(!record.get('checked')){
                      		if (record.get('leaf')) {
                      			files[files.length] = record.get('id');
                          	}else{
                          		record.cascadeBy(function(node) {
                          			if (node.get('leaf')) {
                          				files[files.length] = node.get('id');
                                  	}
                                  });  
                          	}
                      	}else{
                      		if (record.get('leaf')) {
                      			for (var i=0, l=files.length; i<l; i++) {
                                      if(files[i] == record.get('id')) {
                                      	files.splice(i,1);
                                      	break;
                                      }
                                  }
                          	}else{
                          		record.cascadeBy(function(node) {
                          			if (node.get('leaf')) {
                          				for (var i=0, l=files.length; i<l; i++) {
                                              if(files[i] == node.get('id')) {
                                              	files.splice(i,1);
                                              	break;
                                              }
                                          }
                                  	}
                                  });  
                          	}
                      	}
                      }
                  },
                 viewConfig: {
                      multiSelect: true,
                      loadMask: true,
                      loadingText:Share.T('目录读取中，请稍候...'),
                      onCheckboxChange : function(e, t) {  
                          var item = e .getTarget( this .getItemSelector(),  this .getTargetEl()), record;  
                          if (item) {  
                              record = this.getRecord(item);  
                              var check = !record.get('checked');  
                              record .set( 'checked', check);  
                              if (check) {  
                                  record.cascadeBy(function(node) {  
                                      node.set( 'checked', true);  
                                  });  
                              }else {  
                                  record .cascadeBy(function( node) {  
                                      node.set(  'checked', false);  
                                  });  
                              }  
                          }  
                      }  
                  }
		          
                },
                {
                    xtype: 'displayfield',
                    x: 20,
                    y: 0,
                    id:'userRights-permission-username',
                    fieldLabel: Share.T('用户名'),
                    margin: '10 50 10'
                    //width: 300
                }
               
            ]
        });

        me.callParent(arguments);

     /* var mytree= Ext.ComponentMgr.get("userRights-permission-tree");
	  mytree.on('checkchange', function (node, flag) { 
   		 node.checked = flag;  
   		 node.cascadeBy(function (node) {  //获取所有子节点，cascadeBy从当前节点开始向下迭代
        	 node.set('checked', flag);  
  		  });  		
	}, mytree);*/
    }

});